package com.emm.yixun.website.service.marketing;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.HistoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.task.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.model.BusinessAbnorma;
import com.emm.yixun.common.model.PriceScheme;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.BusinessAbnormaDto;
import com.emm.yixun.common.model.dto.PriceSchemeDto;
import com.emm.yixun.common.model.dto.UserDto;
import com.emm.yixun.common.model.enums.BusinessAbnormaEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.BeanUtil;
import com.emm.yixun.marketing.service.BusinessAbnormaServiceFacade;
import com.emm.yixun.marketing.service.PriceSchemeServiceFacade;
import com.emm.yixun.system.service.UserServiceFacade;

@Service
public class PriceSchemeServiceImpl implements PriceSchemeService {
	@Autowired
	private HistoryService historyService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private PriceSchemeServiceFacade priceSchemeServiceFacade;

	@Autowired
	private UserServiceFacade userServiceFacade;
	
	@Autowired
	private BusinessAbnormaServiceFacade businessAbnormaServiceFacade;
	
	private String queryLastCheckName(String processInstanceId){
		List<HistoricActivityInstance> instances = historyService.createHistoricActivityInstanceQuery()
        .processInstanceId(processInstanceId).activityType("userTask").orderByHistoricActivityInstanceStartTime().desc().list();
		if (instances == null || instances.size()==0){
			return "";
		}
		HistoricActivityInstance historicActivityInstance=instances.get(0);
		List<Comment> commentList = taskService.getTaskComments(historicActivityInstance.getTaskId());
		if(null==commentList || commentList.size()==0)return "";
		Comment comment=commentList.get(0);
		UserDto userDto = new UserDto();
		userDto.setUserAccount(comment.getUserId());
		ResponseList<User> userList = userServiceFacade.findByDto(userDto);
		if(userList.isSuccess()&&userList.getEntity()!=null){
			return userList.getEntity().get(0).getUserName();
		}
		return "";
			
	}
	@Override
	public Page<PriceSchemeDto> getPriceSchemeList(
			PriceSchemeDto reqDto, Integer pageSize, Integer pageNum) {
		Page<PriceSchemeDto> response = new Page<PriceSchemeDto>();
		Page<PriceScheme> depositListResDto = priceSchemeServiceFacade.findPageByDto(reqDto, pageSize,
				pageNum);
		List<PriceSchemeDto> priceSchemeDtos = new ArrayList<PriceSchemeDto>();
		
		try {
			if (depositListResDto.getRows() != null
					&& depositListResDto.getRows().size() > 0) {// 查询审批历史记录
				List<PriceScheme> list = depositListResDto.getRows();
				for (PriceScheme priceScheme : list) {
					PriceSchemeDto priceSchemeDto = new PriceSchemeDto();
					BeanUtil.convert2T(priceSchemeDto, priceScheme);
					if(priceScheme.getStatus().equals("10")){
						//当价格方案状态为有效则获取最后节点审核人
						priceSchemeDto.setTaskUserName(queryLastCheckName(priceScheme.getProcInstId()));
					}else if(priceScheme.getStatus().equals("20")){
						//当价格方案状态为无效则获取有效审批最后节点审核人
						priceSchemeDto.setTaskUserName(queryLastCheckName(priceScheme.getProcInstId()));
						//当价格方案状态为无效则获取无效审批最后节点审核人
						BusinessAbnormaDto businessAbnormaDto=new BusinessAbnormaDto();
		        		businessAbnormaDto.setMerchantId(priceScheme.getMerchantId());
		        		businessAbnormaDto.setBusinessId(priceScheme.getId());
		        		businessAbnormaDto.setBusinessType(BusinessAbnormaEnum.BusinessAbnorma_10.getNo());
		        		businessAbnormaDto.setOrders(" t.create_time desc ");
		        		ResponseList<BusinessAbnorma> baList=businessAbnormaServiceFacade.findByDto(businessAbnormaDto);
		        		if(null!=baList.getEntity() && baList.getEntity().size()>0){
		        			BusinessAbnorma businessAbnorma=baList.getEntity().get(0);
		        			priceSchemeDto.setInvalidCheckName(queryLastCheckName(businessAbnorma.getProcInstId()));
		        		}
					}
					
					priceSchemeDtos.add(priceSchemeDto);
				}

			}
			response.setRows(priceSchemeDtos);
			response.setSuccess(true);
			response.setTotal(depositListResDto.getTotal());
			response.setPageSize(depositListResDto.getPageSize());
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(e.getMessage());
		}
		
		return response;
	}

}
